package com.enfi.api.configuration;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

/**
 * @author wind
 * @since  2020/4/3
 */
@Component
@Slf4j
public class MyStartupRunner implements CommandLineRunner {
    @Value("${spring.servlet.multipart.location}")
    private String multipartLocation;
    @Autowired
    private SysConfig sysConfig;
    @Override
    public void run(String... args) throws Exception {
        Map<String, List<String>>  map=sysConfig.getStorage();
        map.forEach((k,v)->{
            if(v!=null){
                v.forEach(t->{
                    initPath(t);
                });
            }
        });
        initPath(multipartLocation);
    }

    public void initPath(String location) {
        Path path = Paths.get(location);
        if (!Files.exists(path)) {
            log.warn("location:{} 不存在，创建", location);
            try {
                Files.createDirectories(path);
            } catch (IOException e) {
                log.warn("initPath error:{}",e.getMessage());
            }
        } else {
            log.warn("location:{} 已存在", location);
        }
    }
}